import state from './state'
export default {
  addGoods(state, payload) {
    let flag = false;
    flag = state.arr.some(function (item) {
      if (item.id == payload.id) {
        item.num++;
        return true;
      } else {
        return false;
      }
    })
    if (!flag) {
      state.arr.push({
        'id': payload.id,
        'name': payload.name,
        'price': payload.price,
        'num': 1
      })
    }
  },
  addNum(state, payload) {
    state.arr[payload].num++;
  },
  reduceNum(state, payload) {
    if (state.arr[payload].num == 1) {
      state.arr.splice(payload, 1);
    } else {
      state.arr[payload].num--;
    }
  },
  deleteGoods(state, payload) {
    state.arr.splice(payload, 1);
  }
}
